//点击获取验证码倒计时 禁用状态
const gitCode = (element, num, callback) => {
  // console.log(element);
  element.innerHTML = num + "秒后重新获取";
  element.style.color = "rgb(118, 118, 118)";
  element.style.background = "#ccc";
  element.style.border = "#ccc";
  element.disabled = "disabled";
  var timer = setInterval(function() {
    num--;
    element.innerHTML = num + "秒后重新获取";
    element.style.color = "rgb(118, 118, 118)";
    element.style.background = "#ccc";
    element.style.border = "#ccc";
    element.disabled = "disabled";
    if (num === 0) {
      element.disabled = "";
      element.style.background = "#07c160";
      element.style.color = "#fff";
      element.innerHTML = "获取验证码";
      clearInterval(timer);
      let code = getCode();
      callback(code);
      return true;
    }
  }, 1000);
};
// 根据Unicode万国码随机生成验证码
function getRandom(min, max) {
  return Math.round(Math.random() * (max - min) + min);
}

function getCode() {
  let code = "";
  for (var i = 0; i < 6; i++) {
    var type = getRandom(1, 3);
    switch (type) {
      case 1:
        code += String.fromCharCode(getRandom(48, 57)); //数字
        break;
      case 2:
        code += String.fromCharCode(getRandom(65, 90)); //大写字母
        break;
      case 3:
        code += String.fromCharCode(getRandom(97, 122)); //小写字母
        break;
    }
  }
  return code;
}

// 正则验证 手机号 和 密码 是否是正确格式
let validation = {
  // 验证手机号
  isphone: phone => {
    return /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(
      phone
    );
  },
  // 验证密码
  ispasword: tel => {
    return /^[a-zA-Z0-9@\$\^\.\*\\?]{6,20}$/.test(tel);
  }
};

module.exports = { gitCode, validation };
